package com.example.shoppingcart.mapper;


import com.example.model.shppingcart.dto.GoodsAndCountDTO;
import com.example.model.shppingcart.dto.ShoppingCartDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface ShoppingCartMapper {
     /**
      * 添加购物车
      *
      * @param id
      * @param goodsAndCountDTOId
      * @return
      */

     @Select("select *,quantity as count from sm.shopping_cart where username=#{username} and product_id =#{goodsAndCountDTOId}")
     ShoppingCartDTO selectUserCart(String username, String goodsAndCountDTOId);
     @Select("select * from sm.goods where product_id = #{id}")
          ShoppingCartDTO selectGoods(GoodsAndCountDTO goodsAndCountDTO);
     @Insert("insert into sm.shopping_cart (user_id, product_id, name, price, photo, quantity, created_time, last_updated_time, is_selected)" +
             "values(#{userId},#{productId},#{name},#{price},#{picture},#{count},now(),now(),null) ")
          void insertCart(ShoppingCartDTO goodsAndCountDTO);

     /**
      * 查询购物车
      * @param username
      * @return
      */
     @Select("select *,photo as picture,quantity as count from sm.shopping_cart where username=#{username}")
     List<ShoppingCartDTO> selectCart(String username);

     /**
      * 更新数量
      * @param count
      * @param userId
      * @param goodId
      */
     @Update("update sm.shopping_cart set quantity = #{count} where username=#{username} and product_id = #{goodId}")
     void updateCart(int count, String username, String goodId);
}
